/**
 * fileName: TabsTag.java
 *     Date: 2012-6-5
 **/
package com.stranger.common.tag.tabs;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @作者 stranger.lin
 *
 * @时间 2012-6-5
 */
public class TabsTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String id;
	private List<Tab> tabs;
	private 	JspWriter out;
	@Override
	public int doStartTag() throws JspException {
		try {
			if (tabs==null) {
				tabs=new ArrayList<Tab>();
			}
			out=pageContext.getOut();
			StringBuffer html=new  StringBuffer("<div id=\""+id+"\">");
			html.append("<ul id=\"_tabs_title_ul_\"></ul>");
		
			out.print(html);
			out.flush();
			//out.close();
		} catch (Exception e) {
			throw new JspException();
		}
		
		return EVAL_BODY_INCLUDE;
	}
	
	
//	<script>
//	$(function() {
//		$( "#tabs" ).tabs();
//	});
//	</script>
	@Override
	public int doEndTag() throws JspException {
		try {
			out=pageContext.getOut();
			StringBuffer html=new  StringBuffer("</div>");
			html.append("<script>\n");
			html.append("$(function() {\n");
			for (Tab tab : tabs) {
				html.append("$(\"#_tabs_title_ul_\").append(\'<li><a href=\"#"+tab.getId()+"\">"+tab.getName()+"</a></li>\');\n");
			}
			
			html.append("$( \"#"+id+"\" ).tabs();\n");
			
			html.append("});\n");
			html.append("</script>");
			out.print(html);
			out.flush();
			//out.close();
			tabs=null;
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		
		
		return EVAL_PAGE;
	}
	
	
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<Tab> getTabs() {
		
		return tabs;
	}
	public void setTabs(List<Tab> tabs) {
		this.tabs = tabs;
	}
	
}
